/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.material.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.material.VO.MaterialVO;
import com.pitop.material.entity.Material;
import com.pitop.material.entity.MaterialState;
import com.pitop.material.service.MaterialSV;
import com.pitop.supplier.service.SupplierSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 供应料材 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/material")
@Slf4j
@Api(value = "供应料材控制器", description = "供应料材控制器")
public class MaterialCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private MaterialSV materialSV;

    @Resource
    private SupplierSV supplierSV;

    /**
     * 查询Material一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query"),
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public MaterialVO load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }

        MaterialVO materialVO = materialSV.load(id, code);
        log.info(JSON.toJSONString(materialVO));
        return materialVO;
    }


    /**
     * 根据条件id查询Material一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public MaterialVO loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        MaterialVO materialVO = materialSV.loadById(id);
        log.info(JSON.toJSONString(materialVO));
        return materialVO;
    }

    /**
     * 根据条件code查询Material一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public MaterialVO loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        MaterialVO materialVO = materialSV.loadByCode(code);
        log.info(JSON.toJSONString(materialVO));
        return materialVO;
    }

    /**
     * 根据条件name查询Material一个详情信息
     *
     * @param name 供应材料名称
     * @return BeanRet
     */
    @ApiOperation(value = "查询Material一个详情信息", notes = "查询Material一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "供应材料名称", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/name/{name}")
    @ResponseBody
    public Material loadByName(@PathVariable String name) {
        if (name == null) {
            return null;
        }
        Material material = materialSV.loadByName(name);
        log.info(JSON.toJSONString(material));
        return material;
    }


    /**
     * 查询Material信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Material信息集合", notes = "查询Material信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "materialCategoryCode", value = "材料类型编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query")
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<MaterialVO> list(@RequestBody @ApiIgnore Material material, Integer curPage, Integer pageSize) {
        Page<MaterialVO> page = new Page<>(pageSize, curPage);
        List<MaterialVO> materialVOs = materialSV.list(material, page.genRowStart(), page.getPageSize());
        page.setVoList(materialVOs);
        log.info(JSON.toJSONString(page));
        return materialVOs;
    }


    /**
     * 查询Material信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Material信息集合", notes = "查询Material信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
    })
    @GetMapping(value = "/listForAssembly")
    @ResponseBody
    public List<MaterialVO> listForAssembly(String assemblyCode, String state, String name, Integer curPage, Integer pageSize) {
        Page<MaterialVO> page = new Page<>(pageSize, curPage);
        List<MaterialVO> materials = materialSV.listForAssembly(assemblyCode, state, name, page.genRowStart(), page.getPageSize());
        page.setVoList(materials);
        log.info(JSON.toJSONString(page));
        return materials;
    }


    /**
     * 查询Material信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Material信息集合", notes = "查询Material信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
    })
    @GetMapping(value = "/listForBom")
    @ResponseBody
    public List<MaterialVO> listForBom(String bomCode, String state, Integer curPage, Integer pageSize, String name) {
        Page<MaterialVO> page = new Page<>(pageSize, curPage);
        List<MaterialVO> materials = materialSV.listForBom(bomCode, state, name, page.genRowStart(), page.getPageSize());
        page.setVoList(materials);
        log.info(JSON.toJSONString(page));
        return materials;
    }


    /**
     * 查询Material信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Material信息集合", notes = "查询Material信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<MaterialVO> listByPk(Long id, String code, String suppierCode, Integer curPage, Integer pageSize) {
        Page<MaterialVO> page = new Page<MaterialVO>(pageSize, curPage);
        List<MaterialVO> materials = materialSV.list(id, code, suppierCode, page.genRowStart(), page.getPageSize());
        int total = materialSV.count(id, code, suppierCode);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return materials;
    }

    /**
     * 统计Material信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Material信息数量根据主键", notes = " 统计Material信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code, String suppierCode) {
        return materialSV.count(id, code, suppierCode);
    }

    /**
     * 统计Material信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Material信息数量", notes = "统计Material信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "编号", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "specifications", value = "规格 厘米，克等计量单位", paramType = "query"),
            @ApiImplicitParam(name = "brandCode", value = "品牌编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCategoryCode", value = "材料类型编码", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述 64个汉字描述", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Material material) {
        return materialSV.count(material);
    }

    /**
     * 根据组件编码和状态查询中条数
     *
     * @return 总条数
     */
    @ApiOperation(value = "根据组件编码和状态查询中条数", notes = "根据组件编码和状态查询中条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assemblyCode", value = "组件编码e", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),

    })
    @GetMapping(value = "/countForAssembly")
    @ResponseBody
    public Integer countForAssembly(String assemblyCode, String state, String name) {
        return materialSV.countForAssembly(assemblyCode, state, name);
    }

    /**
     * 根据bom编码和状态查询中条数
     *
     * @return 总条数
     */
    @ApiOperation(value = "根据bom编码和状态查询中条数", notes = "根据bom编码和状态查询中条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),

    })
    @GetMapping(value = "/countForBom")
    @ResponseBody
    public Integer countForBom(String bomCode, String state, String name) {
        return materialSV.countForBom(bomCode, state, name);
    }

    /**
     * 统计Material信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Material信息数量", notes = "统计Material信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "编号", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "specifications", value = "规格 厘米，克等计量单位", paramType = "query"),
            @ApiImplicitParam(name = "brandCode", value = "品牌编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCategoryCode", value = "材料类型编码", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述 64个汉字描述", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping(value = "/countByCategoryName")
    @ResponseBody
    public Integer countByCategoryName(@RequestBody Material material) {
        return materialSV.countByCategoryName(material);
    }


    /**
     * 创建供应料材(Material)
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建供应料材", notes = "创建供应料材")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "编号", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "specifications", value = "规格 厘米，克等计量单位", paramType = "query"),
            @ApiImplicitParam(name = "brandCode", value = "品牌编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCategoryCode", value = "材料类型编码", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述 64个汉字描述", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1009)：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "原厂型号编号", paramType = "query"),
            @ApiImplicitParam(name = "materialVerion", value = "物料版本号", paramType = "query"),
            @ApiImplicitParam(name = "star", value = "供应料星级", paramType = "query"),
            @ApiImplicitParam(name = "fob", value = "价格", paramType = "query"),
            @ApiImplicitParam(name = "untaxedPrice", value = "未含税价格", paramType = "query"),
            @ApiImplicitParam(name = "taxPrice", value = "含税价格", paramType = "query"),
            @ApiImplicitParam(name = "dollarPrice", value = "美元价格", paramType = "query"),
            @ApiImplicitParam(name = "taxRate", value = "税率", paramType = "query"),
            @ApiImplicitParam(name = "moq", value = "最小订货量 MOQ", paramType = "query"),
            @ApiImplicitParam(name = "cycleTime", value = "生产周期", paramType = "query"),
            @ApiImplicitParam(name = "materialUnit", value = "单位", paramType = "query"),
            @ApiImplicitParam(name = "materialCurrency", value = "货币", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "管理员账户", paramType = "query"),
            @ApiImplicitParam(name = "accountCode", value = "管理员账户编码", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public Material build(@RequestBody @ApiIgnore Material material, String accout,String accountCode) {
        return materialSV.insert(material, accout, accountCode);
    }


    /**
     * 修改Material
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Material", notes = "修改Material")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "编号", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "specifications", value = "规格 厘米，克等计量单位", paramType = "query"),
            @ApiImplicitParam(name = "brandCode", value = "品牌编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCategoryCode", value = "材料类型编码", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述 64个汉字描述", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1009)：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "原厂型号编号", paramType = "query"),
            @ApiImplicitParam(name = "materialVerion", value = "物料版本号", paramType = "query"),
            @ApiImplicitParam(name = "star", value = "供应料星级", paramType = "query"),
            @ApiImplicitParam(name = "fob", value = "价格", paramType = "query"),
            @ApiImplicitParam(name = "untaxedPrice", value = "未含税价格", paramType = "query"),
            @ApiImplicitParam(name = "taxPrice", value = "含税价格", paramType = "query"),
            @ApiImplicitParam(name = "dollarPrice", value = "美元价格", paramType = "query"),
            @ApiImplicitParam(name = "taxRate", value = "税率", paramType = "query"),
            @ApiImplicitParam(name = "moq", value = "最小订货量 MOQ", paramType = "query"),
            @ApiImplicitParam(name = "cycleTime", value = "生产周期", paramType = "query"),
            @ApiImplicitParam(name = "materialUnit", value = "单位", paramType = "query"),
            @ApiImplicitParam(name = "materialCurrency", value = "货币", paramType = "query")
    })
    @PostMapping("/modify")
    @ResponseBody
    public Material modify(@RequestBody @ApiIgnore Material material) {
        material.setUpdateTime(new Date());
        materialSV.modify(material);
        return material;
    }

    /**
     * 修改Material
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Material", notes = "修改Material")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1009)：启用 Enable,禁用 Disable", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public MaterialVO updateState(String code, MaterialState state) {
        materialSV.updateByCode(code, state);
        return materialSV.loadByCode(code);
    }


    /**
     * 删除Material
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Material", notes = "删除Material")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "suppierCode", value = "供应商编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code, String suppierCode) {
        materialSV.delete(id, code, suppierCode);
        return BeanRet.create(true, "删除Material成功");
    }


    /**
     * 上传Excel文件，批量导入数据
     * 1.生成供应商类型sql,保存着supplier.txt文件中
     * 2.把供应商类型插入到数据库中，插入过程去重
     * 3.生成支付方式类型sql,保存到supplier.txt文件中
     * 4.把支付方式插入到数据库中，插入过程去重
     * 5.生成供应商sql,同时根据供应类型，支付方式找到对应的供应商类型编码和支付方式编码
     * 6.把供应插入到数据库中，插入过程去重，并统计失败数据，成功数据保存到supplier.sql文件中
     *
     * @return BeanRet
     */
    @ApiOperation(value = "上传Excel文件，批量导入材料", notes = "上传Excel文件，批量导入数据")
    @PostMapping("/create")
    @ResponseBody
    public BeanRet create(MultipartFile limitFile) {
        try {
            materialSV.importMaterial(limitFile.getInputStream(), limitFile.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            return BeanRet.create();
        }
        return BeanRet.create(true, "添加成功");
    }

}
